<template>
  <div>
    <!-- 面包屑导航区 -->
    <bread-crumb :breadPath="breadPath"></bread-crumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索框 和 添加按钮  -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage" >添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据列表区 -->
      <el-table :data="goodList"   border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px" align="center"></el-table-column>
        <el-table-column label="商品重量(千克)" prop="goods_weight" width="120px" align="center"></el-table-column>
        <el-table-column label="创建时间" width="200px" align="center">
          <template v-slot:default={row}>
            {{row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px" align="center">
          <template v-slot:default={row}>
            <!-- v-slot:default="scope" -->
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(row.goods_id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodById(row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 30]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  import breadCrumb from '@/components/breadcrumb'
  import { getGoods, deleteGoodInfo} from '@/api/goods.js';

  export default {
    data() {
      return { 
        // 面包屑 传参
        breadPath: ['商品管理', this.$route.query.bread],
        // 搜索框的绑定数据
        queryInfo: {
          // 搜索用的参数
          query: '',
          // 当前页数
          pagenum: 1,
          // 每页显示的条数
          pagesize: 10
        },
        // 商品数据列表 
        goodList: [],
        // 商品列表数据的总数
        total: 0
      }
    },
    created() {
      this.getGoodsList();
    },
    methods: {
      // 获取商品数据列表
      async getGoodsList() {
        try {
          const { data: res } = await getGoods(this.queryInfo);
          this.goodList = res.goods;
          this.total = res.total;
          // console.log(this.goodList);
        } catch (err) {
          console.log(err);
        }
      },
      // 点击 添加按钮 跳转到 添加页面
      goAddPage() {
        this.$router.push({ path: '/goods/add' , query: { bread:'添加商品' } })
      },
      // 显示 修改 对话框
      showEditDialog(id) {
        console.log(id);
      },
      // 显示 删除 消息确认框，并 处理删除请求
      async removeGoodById(id) {
        const confirmResult = await this.$confirm('此操作将永久删除该商品数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err);
        // console.log(confirmResult);
        if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除');
        }
        deleteGoodInfo(id)
          .then(()=>{
            this.$message.success('已删除该商品');
            this.getGoodsList();
          }).catch(() => {this.$message.error('操作失败！')})
      },
      // 监听 pagesize 改变的事件 
      handleSizeChange(newSize) {
        // console.log(newSize);
        this.queryInfo.pagesize = newSize;
        this.getGoodsList();
      },
      // 监听 页码值 改变的事件
      handleCurrentChange(newPage) {
        // console.log(newPage);
        this.queryInfo.pagenum = newPage;
        this.getGoodsList();
      },
    },
    components: {
      breadCrumb,
    }
  }
</script>

<style lang="less" scoped>

</style>